/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.google
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.d4a5.mapping.core.Bounds;
	import com.d4a5.mapping.core.Utils;
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.interfaces.IPolygon;
	import com.google.maps.overlays.EncodedPolylineData;
	import com.google.maps.overlays.PolygonOptions;
	import com.google.maps.styles.FillStyle;
	import com.google.maps.styles.GradientStyle;
	import com.google.maps.styles.StrokeStyle;
	
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	
	import mx.core.mx_internal;
	import mx.graphics.Stroke;
	
	public class wmgPolygon extends wmgBaseOverlay implements IPolygon
	{
		protected var _rings:Array;
		protected var _polygonOptions:PolygonOptions;
		
		
		
		public function wmgPolygon( geometry:Object = null , options:PolygonOptions = null )
		{
			super();
			if( options ){
				this.setOptions( options );
			}
			
			
			if( geometry is AmfObject ){
				this._amf = geometry as AmfObject;
				this.attributes = this._amf.attributes;
			}
			else if( geometry is Array ){
				this.amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolygon , geometry , this.attributes );
				this._rings = geometry as Array;
			}
			
		}
		
		override public function set amf( a:AmfObject ) : void {
			super.amf = a;
			this._rings = null;
			if( a ){
				this.attributes = a.attributes;
			}
		}
		
		public function getInnerVertex(arg0:int, arg1:int):LatLng
		{
			return this.getVertex( arg0 + 1 , arg1 );
		}
		
		public function getOuterVertexCount():int
		{
			return this.getVertexCount(0);
		}
	
		public function setPolylineFromEncoded(arg0:int, arg1:EncodedPolylineData):void
		{
			throw new Error( "Error: setPolylineFromEncoded is not implemented" );
		}
		
		public function isHidden():Boolean
		{
			return !(this.visible);
		}
		
		public function getInnerVertexCount(arg0:int):int
		{
			return this.getVertexCount( arg0 + 1 );
		}
		
		public function getOptions():PolygonOptions
		{
			var options:PolygonOptions = this.retrieveOptions() as PolygonOptions;
			
			if( this._polygonOptions ){
				if( options ){
					return PolygonOptions.merge( [ this._polygonOptions , options ] );
				}
				return this._polygonOptions;
			}
			else if( options ){
				return options;
			}
			
			return PolygonOptions.getDefaultOptions();
		}
		
		public function getOuterVertex(arg0:int):LatLng
		{
			return this.getVertex( 0 , arg0 );
		}
		
		public function hide():void
		{
			this.visible = false;
		}
		
		public function getVertexCount(arg0:int):int
		{
			return this.rings[arg0].length;
		}

		public function removePolyline(arg0:int):void
		{
			this.rings.splice( arg0 , 1 );
			
			var storedAsFloat:Boolean = ( this._amf ) ? this._amf.storedAsFloat : false;
			this._amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolygon , this.rings , this.attributes , storedAsFloat );
		}
		
		public function getVertex(arg0:int, arg1:int):LatLng
		{
			return this.rings[arg0][arg1] as LatLng;
		}
	
		public function getLatLngBounds():LatLngBounds
		{
			if( this._amf ){
				var b:Bounds = this._amf.bounds;
				var pt:Point = new Point();
				Utils.WebMercatorToGeographicFaster( b.x.min , b.y.min , pt );
				var sw:LatLng = new LatLng( pt.y , pt.x );
				
				Utils.WebMercatorToGeographicFaster( b.x.max , b.y.max , pt );
				var ne:LatLng = new LatLng( pt.y , pt.x );
				
				return new LatLngBounds( sw , ne );
			}
			return null;
		}

		public function setPolyline(arg0:int, arg1:Array):void
		{
			this.rings.splice( arg0 , 0 , arg1 );
			
			var storedAsFloat:Boolean = ( this._amf ) ? this._amf.storedAsFloat : false;
			this._amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolygon , this.rings , this.attributes , storedAsFloat );
		}
	
		public function getPolylineCount():int
		{
			return this.rings.length;
		}
	
		public function setOptions(arg0:PolygonOptions):void
		{
			this._polygonOptions = arg0;
		}
		
		public function show():void
		{
			this.visible = true;
		}
	
		public function getInnerPolylineCount():int
		{
			return this.rings.length - 1;
		}
		
		public function get rings():Array {
			if( this._rings == null && this._amf ){
				var geographicPt:Point = new Point();
				
				var readCoord:Function = ( this._amf.storedAsFloat ) ? this._amf.geometry.readFloat : this._amf.geometry.readDouble;
				
				this._amf.geometry.position = 0;
				this._amf.geometry.endian = Endian.BIG_ENDIAN;
				
				var outer:Array = new Array();
				var ringsCount:int = this._amf.ringsCount;
				while( ringsCount-- ){
					var inner:Array = new Array();
					var numPoints:int = this._amf.geometry.readInt();
					
					while( numPoints-- ){
						Utils.WebMercatorToGeographicFaster( readCoord() , readCoord() , geographicPt );
						inner.push( new LatLng( geographicPt.y , geographicPt.x ) );
					}
					
					outer.push( inner );
				}
				this._rings = outer;
				
			}
			return this._rings;
		}
		
		
		
		override protected function draw( m:Matrix ):void {
			this.graphics.clear();
			this._drawn = false;
			
			var options:PolygonOptions = this.getOptions();
			var stroke:StrokeStyle = options.strokeStyle;
			var fill:FillStyle = options.fillStyle;
			var gFill:GradientStyle;
			if( fill ){
				gFill = options.fillStyle.gradient;
			}
			this.toolTip = options.tooltip;
			
			var drawOutline:Boolean = false;
			if( stroke && stroke.alpha > 0 && stroke.thickness > 0 ){
				this.graphics.lineStyle( stroke.thickness as Number, stroke.color as uint , stroke.alpha as Number , stroke.pixelHinting );
				drawOutline = true;
			}
			
			if( gFill ){
				this.graphics.beginGradientFill( gFill.type , gFill.colors , gFill.alphas , gFill.ratios , gFill.matrix , gFill.spreadMethod , gFill.interpolationMethod , gFill.focalPointRatio );
				amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
				this.graphics.endFill();
				this._drawn = true;
			}
			else if( fill ){
				this.graphics.beginFill( fill.color as uint , fill.alpha as Number );
				amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
				this.graphics.endFill();
				this._drawn = true;
			}
			else if( drawOutline ){
				amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
				this._drawn = true;
			}
			
		}
		
		
	}
}